﻿<?php
    include ("../lib/BLL/DmNganhBLL.php");
    $tt="";
    $thongbao="";
    $ma="";
    $ten="";
    $tenvt="";
    $gioithieu="";
    $btnName="Thêm";
    $nganh_bll=new DmNganhBLL();
    $ds_nganh=array();
        
    if (isset($_POST['timkiem'])) {
        $keysearch="";
		$where="";
        if(isset($_POST['txtkey'])){
            $keysearch=$_POST['txtkey'];
            $where="WHERE".$nganh_bll->buildSearchQueryLike($keysearch);
        }
    }
    
    if(!isset($_POST['timkiem'])||($_POST['txtkey']=="")){
        $where="WHERE 1=1";
    }
    if(isset($_GET['ma'])){
        $btnName="Sửa";
        $madm_nganh=$_GET['ma'];
        $ds_nganh=$nganh_bll->getItemNganh($madm_nganh);
        $ma=$ds_nganh['madm_nganh'];
        $ten=$ds_nganh['ten_nganh'];
        $tenvt=$ds_nganh['ten_vt_nganh'];
        $gioithieu=$ds_nganh['gioithieu_nganh'];
        $tt="readonly";
    }
    if(isset($_POST['btnLuu'])){
        $nganh=new DmNganhDAL();
        $madm_nganh=$_POST['madmnganh'];
        $ten_nganh=$_POST['tennganh'];
        $ten_vt_nganh=$_POST['tenvtnganh'];
        $gioithieu_nganh=$_POST['gioithieunganh'];
        if($madm_nganh!=""&&$ten_nganh!=""&&$ten_vt_nganh!=""){
            if($btnName=="Thêm"){
                $nganh->setdmNganh($madm_nganh,$ten_nganh,$ten_vt_nganh,$gioithieu_nganh);
                $kq=$nganh_bll->InsertNganh($nganh);
                if($kq==true){
                    header("location: chuongtrinhdaotao-dmnganh");
                }
            }else{
                $nganh->setdmNganh($ma,$ten_nganh,$ten_vt_nganh,$gioithieu_nganh);
                $kq=$nganh_bll->updateNganh($nganh);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-dmnganh");
                }
            }
        }
    }
    if(isset($_GET['xoama'])){
        $maxoa=$_GET['xoama'];
        $kq=$nganh_bll->deleteNganh($maxoa);
        if ($kq == true){
            header("location: chuongtrinhdaotao-dmnganh");
        }
    }
    if(isset($_POST['xoachon'])){
        if(isset($_POST['checkall'])){
            $kq=$nganh_bll->deleteAll();
            if ($kq == true){
                header("location: chuongtrinhdaotao-dmnganh");
            }
        }
        if(isset($_POST['checkbox'])){
            $delete=$_POST['checkbox'];
            foreach($delete as $madm_nganh){
                $kq=$nganh_bll->deleteNganh($madm_nganh);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-dmnganh");
                }
            }
        }
    }

    $ds_nganh=$nganh_bll->timkiem($where);
    $totalRecords=count($ds_nganh);        
    $p=(isset($_GET['p']))?$_GET['p']:1;
    $limit=35;                       
    $offset=($p-1)*$limit;
    $ds_nganh=$nganh_bll->getPages($where,$offset,$limit);
    $url="chuongtrinhdaotao-dmnganh";
    $pages=$nganh_bll->paging($totalRecords,$limit,$url,'pagers');  

    $smarty->assign('p',$p);
    $smarty->assign('pages',$pages);
    $smarty->assign('ma',$ma);
    $smarty->assign('btnName',$btnName);
    $smarty->assign('ten',$ten);
    $smarty->assign('tenvt',$tenvt);
    $smarty->assign('gioithieu',$gioithieu);
    $smarty->assign('tt',$tt);
    $smarty->assign('thongbao',$thongbao);
    $smarty->assign('ds_nganh',$ds_nganh);
    $smarty->display('chuongtrinhdaotao/dmNganh.tpl');
?>